/**
 ******************************************************************************
 * @file    lpuart.c
 * @author  hyseim software Team
 * @date    18-Aug-2023
 * @brief   This file provides all the tmp functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "lpuart.h"
#include "common.h"


/** @addtogroup IM110GW_Driver
 * @{
 */

/** @defgroup LPUART
 * @brief LPUART driver modules
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup LPUART_Private_Functions
  * @{
  */

/**
 * @brief  Deinitializes the LPUART peripheral registers to their default reset values.
 * @return None
 */
void LPUART_DeInit(void)
{
    im_write32((CRG_BASE + 0x002), (0x1 << 3) | ((0x1 << 3) << 16));
    im_write32((CRG_BASE + 0x002), (0X0 << 3) | ((0x1 << 3) << 16));
}

/**
 * @brief  Config the LPUART Baudrate.
 * @param  Div: LPUART Baudrate = Pclk / Div.
 *         Div Mini Value = 16.
 * @return None
 */
void LPUART_BaudrateConfig(uint32_t Div)
{
    if (Div < 16) {
        LPUART->DIV = 16;
    }
    else if (Div > 0xfffff) {
        LPUART->DIV = 0xfffff;
    }
    else {
        LPUART->DIV = Div;
    }
}

/**
 * @brief  Enables or disables the LPUART receiver block.
 * @param  NewState: new state of the receiver block.
 *         This parameter can be: ENABLE or DISABLE.
 * @return None
 */
void LPUART_ReceiverCmd(FunctionalState_t NewState)
{
    if (NewState != DISABLE) {
        LPUART->CTRL |= LPUART_CTRL_RXEN;
    }
    else {
        LPUART->CTRL &= ~LPUART_CTRL_RXEN;
    }
}

/**
 * @brief  Enables or disables the LPUART transmitter block.
 * @param  NewState: new state of the transmitter block.
 *         This parameter can be: ENABLE or DISABLE.
 * @return None
 */
void LPUART_TransmitterCmd(FunctionalState_t NewState)
{
    if (NewState != DISABLE) {
        LPUART->CTRL |= LPUART_CTRL_TXEN;
    }
    else {
        LPUART->CTRL &= ~LPUART_CTRL_TXEN;
    }
}

/**
 * @brief  Write data to THR register.
 * @param  Data: the data to write.
 * @return None
 */
void LPUART_WriteData(uint16_t Data)
{
    LPUART->THR = Data;
}

/**
 * @brief  Read data from RBR register.
 * @return The received data
 */
uint16_t LPUART_ReadData(void)
{
    return LPUART->RBR;
}

/**
 * @brief  Enables or disables the specified LPUART interrupts.
 * @param  LPUART_IT: specifies the LPUART interrupt source to be enabled or disabled. 
 *         This parameter can be a combination of the following values:
 *         @arg @ref LPUART_IT_TX: Transmit Completion Interrupt.
 *         @arg @ref LPUART_IT_RX: Receive Completion Interrupt.
 *         @arg @ref LPUART_IT_TXO: Transmit Overrun Interrupt.
 *         @arg @ref LPUART_IT_RXO: Receive Overrun Interrupt.
 * @param  NewState: new state of the specified LPUART interrupt.
 *         This parameter can be: ENABLE or DISABLE.
 * @return None.
 */
void LPUART_ITConfig(uint8_t LPUART_IT, FunctionalState_t NewState)
{
    if (NewState != DISABLE) {
        LPUART->CTRL |= (LPUART_IT << 2);
    }
    else {
        LPUART->CTRL &= ~(LPUART_IT << 2);
    }
}

/**
 * @brief  Checks whether the specified LPUART interrupt has occurred or not.
 * @param  LPUART_IT: specifies the LPUART interrupt source to check.
 *         This parameter can be one of the following values:
 *         @arg @ref LPUART_IT_TX: Transmit Completion Interrupt.
 *         @arg @ref LPUART_IT_RX: Receive Completion Interrupt.
 *         @arg @ref LPUART_IT_TXO: Transmit Overrun Interrupt.
 *         @arg @ref LPUART_IT_RXO: Receive Overrun Interrupt.
 * @return The new state of LPUART_IT (SET or RESET).
 */
ITStatus_t LPUART_GetITStatus(uint8_t LPUART_IT)
{
    ITStatus_t bitstatus = RESET;

    if (LPUART->ISR & LPUART_IT) {
        bitstatus = SET;
    }
    else {
        bitstatus = RESET;
    }
    return bitstatus;
}

/**
 * @brief  Clears the LPUART interrupt pending bits.
 * @param  LPUART_IT: specifies the interrupt pending bit to clear.
 *         This parameter can be a combination of the following values:
 *         @arg @ref LPUART_IT_TX: Transmit Completion Interrupt.
 *         @arg @ref LPUART_IT_RX: Receive Completion Interrupt.
 *         @arg @ref LPUART_IT_TXO: Transmit Overrun Interrupt.
 *         @arg @ref LPUART_IT_RXO: Receive Overrun Interrupt.
 * @return None.
 */
void LPUART_ClearIT(uint8_t LPUART_IT)
{
    LPUART->ISR = LPUART_IT;
}

/**
 * @brief  Get LPUART's Line status.
 * @return LPUART's Line status.
 */
uint32_t LPUART_GetLineStatus(void)
{
    return LPUART->LSR;
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */
